package com.exercise.ThreadAttribute;

public class ThreadStateExample {
    public static void main(String[] args) throws InterruptedException {
        Thread thread = new Thread(() -> {
            try {
                //进入TIMED_WAITING状态
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        //NEW
        System.out.println("Thread state before start: " + thread.getState());
        thread.start();
        // RUNNABLE
        System.out.println("After start: " + thread.getState());
        Thread.sleep(100);
        // TIMED_WAITING
        System.out.println("During sleep: " + thread.getState());
        // Let main thread wait for the thread to finish
        thread.join();
        System.out.println("After completion: " + thread.getState()); // TERMINATED
    }

}
